package com.learning.crpc.consummer;

import com.learning.crpc.annotation.CrpcReference;
import com.learning.crpc.model.CrpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @program: diy_rpc
 * @description:
 * @author: CaoYong
 * @create: 2021-07-01 16:36
 **/
public class CrpcInvocationHandler implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("toString".equals(method.getName())) {
            return proxy.toString();
        }

        CrpcRequest crpcRequest = new CrpcRequest();
        crpcRequest.setClassName(method.getDeclaringClass().getName());
        crpcRequest.setMethodName(method.getName());
        crpcRequest.setParameterTypes(method.getParameterTypes());
        crpcRequest.setArguments(args);

        CrpcConnection connect = NettyConsumerClient.connect("127.0.0.1", 8081);
        connect.call(crpcRequest);

        return "请求发送成功";
    }
}
